const app = getApp();
// const { xianquArr } = require('../../utils/city');
let xianquArr = undefined;
import { jiatuiRequest } from "/utils/requests";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showContact: true,
    suspendShow: true,
    showIcon: true,
    bgcolor: '',
    form: true,
    table: false,
    options: {
      s1: 'y',
      xiangmu: "sk",
      lx: "sk",
      selectNo: 'y',
    },
    yl: 1,
    gonghao: '无',
    fenxiao: '无',
    laiyuan: '中公AI职位库',
    chaxunjg: '',
    cxlists: [],
    showIcon: !0,
    isShowAuth: !1,
    isShowCdNav: !1,
    onReachBottomNum: 0,
    listId: 1,
    lists: [{
      'title': '按个人条件查询',
      "zt": 'on'
    }, {
      'title': '按职位信息查询',
      "zt": ''
    }],
    nav: 1,
    zwid: 12,
    actid: 17122,
    name: '',
    title: '',
    cxlists: [], //存放比对过后的所有数据
    cxlists1: [], //存放临时的20条
    collectLists: [], //收藏列表--用于比对，输出当前收藏职位
    collectLen: 0,
    filed: {},
    file: [],
    str1: '',
    str2: '',
    str3: '',
    str1Arr: [],
    str11: [],
    str2Arr: [],
    str22: [],
    str3Arr: [],
    str33: [],
    cxFlag: false,
    zyLevel: 2,
    zylevel2: '',
    zylevel1: '',
    zylevel0: '',
    zydata: {},
    grdata: {
      tabnum: 1
    },
    grscdata: {
      flag: true,
      limits: 60,
      tabnum: 3
    }, //个人收藏条件
    qxdata: {
      flag: true,
      tabnum: 3
    }, //取消个人收藏条件
    areaRst: '',
    xianquRst: '',
    xueliRst: '',
    xueweiRst: '',
    zldxRst: '',
    zzmmRst: '',
    sexRst: '',
    sfyjRst: '',
    zgzsRst: '',
    cityArr: [{
      id: "2_1",
      text: "不限"
    }, {
      id: "2_2",
      text: "省直"
    }, {
      id: "2_3",
      text: "石家庄市"
    }, {
      id: "2_4",
      text: "保定市"
    }, {
      id: "2_5",
      text: "唐山市"
    }, {
      id: "2_6",
      text: "承德市"
    }, {
      id: "2_7",
      text: "邯郸市"
    }, {
      id: "2_8",
      text: "沧州市"
    }, {
      id: "2_9",
      text: "秦皇岛市"
    }, {
      id: "2_10",
      text: "张家口市"
    }, {
      id: "2_11",
      text: "廊坊市"
    }, {
      id: "2_12",
      text: "衡水市"
    }, {
      id: "2_13",
      text: "邢台市"
    }],
    cityIndex: 0,
    xianquArr: [],
    xueliArr: [{
      id: "3_1",
      text: "不限"
    }, {
      id: "3_2",
      text: "高中"
    }, {
      id: "3_3",
      text: "中专"
    }, {
      id: "3_4",
      text: "专科"
    }, {
      id: "3_5",
      text: "本科"
    }, {
      id: "3_6",
      text: "研究生"
    }],
    bklx: [{
      id: "5_1",
      text: "综合岗"
    }, {
      id: "5_2",
      text: "教育岗"
    }, {
      id: "5_3",
      text: "医疗岗"
    }],
    xueweiArr: [{
      id: "6_1",
      text: "不限"
    }, {
      id: "6_2",
      text: "学士"
    }, {
      id: "6_3",
      text: "硕士"
    }, {
      id: "6_4",
      text: "博士"
    }],
    sexArr: [{
      id: "7_1",
      text: "女"
    }, {
      id: "7_2",
      text: "男"
    }],
    zzmmArr: [{
      id: "8_1",
      text: "共青团员"
    }, {
      id: "8_2",
      text: "中共党员"
    }, {
      id: "8_3",
      text: "群众"
    }, {
      id: "8_4",
      text: "不限"
    }],
    zldxArr: [{
      id: "9_1",
      text: "2年以上基层工作经历"
    }, {
      id: "9_2",
      text: "2年以上相关工作经历"
    }, {
      id: "9_3",
      text: "服务基层项目人员"
    }, {
      id: "9_4",
      text: "北京冬(残)奥会志愿者"
    }, {
      id: "9_5",
      text: "退役运动员"
    }, {
      id: "9_6",
      text: "不限"
    }],
    sfyjArr: [{
      id: "10_1",
      text: "是"
    }, {
      id: "10_2",
      text: "否"
    }],
    zgzsArr1: [
      {id: 1, text: '教师资格证', checked: false},
      {id: 2, text: '会计证', checked: false},
      {id: 3, text: '法律职业资格证', checked: false},
      {id: 4, text: '执业医师资格证', checked: false},
      {id: 5, text: '执业药师资格证', checked: false},
      {id: 6, text: '护士执业资格证', checked: false},
      {id: 7, text: '住院医师规范化培训合格证', checked: false},
      {id: 8, text: '建造师执业资格证', checked: false},
      {id: 9, text: '消防工程师证', checked: false},
      {id: 10, text: '监理工程师资格证', checked: false},
      {id: 11, text: '计算机技术与软件专业技术资格（水平）证', checked: false},
      {id: 12, text: '其他', checked: false},
      {id: 13, text: '无', checked: false},
    ],
    zgzsArr: [],
    xiangmuArr: [{
      id: 1,
      text: '国家公务员'
    }, {
      id: 2,
      text: '河北公务员'
    }, {
      id: 3,
      text: '事业单位'
    }, {
      id: 4,
      text: '教师招聘'
    }, {
      id: 5,
      text: '特岗教师'
    }, {
      id: 6,
      text: '医疗招聘'
    }, {
      id: 7,
      text: '银行招聘'
    }, {
      id: 8,
      text: '国企招聘'
    }, {
      id: 9,
      text: '选调生'
    }, {
      id: 10,
      text: '三支一扶'
    }, {
      id: 11,
      text: '农信社'
    }, {
      id: 12,
      text: '社区工作者'
    }, {
      id: 13,
      text: '招警'
    }, {
      id: 14,
      text: '辅警'
    }, {
      id: 15,
      text: '公益岗'
    }, {
      id: 16,
      text: '军队文职'
    }, {
      id: 17,
      text: '遴选'
    }],
    xiangmuRes: '',
    fypage: 1,
    zcpages: 1,
    bklxvalue: '',
    zhuanyeFlag: false,
    wx: '',
    myPhone: '',
    isSydw: false,
    limits: 20,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData(await app.methods.myPhone()); // 获取本地存储手机号
    if(!this.data.myPhone) {
      await app.methods.getPhoneNumber()
      this.setData({
        isShowAuth: !0
      })
    }
    xhs.showShareMenu({
      shareTypes: 'normal',
    })
    let links, jxOptions;
    if (options.scene) {
      options.scene = options.scene.replace(/%253D/g, ".");
      options.scene = options.scene.replace(/%2526/g, "-");
      jxOptions = await app.methods.getOPtions(options);
    }
    let id = options.scene ? jxOptions.id : options.id;
    options.id = id;
    links = await app.methods.getLink(id);
    options.fx = options.scene ? jxOptions.fx : options.fx;
    options.gh = options.scene ? jxOptions.gh : options.gh;
    options.actid = links.actid ? links.actid : '57058';
    options.lx = links.lx ? links.lx : '';
    options.name = links.name ? links.name : '中公AI职位库';
    if (options.actid == undefined) {
      options.actid = 57058
    }
    if (options.yl != undefined) {
      if (options.yl.length > 0) {
        this.setData({
          yl: Number(options.yl)
        })
      }
    }
    if (options.lx != undefined) {
      if (options.lx.length > 0) {
        this.setData({
          isSydw: true
        })
      }
    }
    this.setData({
      options,
    })
    // await this.getZsLists(); // 获取证书列表
    this.setData(await app.methods.getIdentity(options)); // 获取个人身份信息
    if (this.data.getIdentity.crm) {
      options.wxh = this.data.getIdentity.crm
    } else {
      options.wxh = options.gh
    }
    console.log(options)
    this.setData(await app.methods.getWechat(options)); // 获取个人身份信息
    this.setData(await app.methods.guishudi(options)); // 获取归属地
    let fenbu = this.data.fenbu;
    let fenxiao = this.data.fenxiao;
    let getIdentity = this.data.getIdentity;
    getIdentity.fenbu = fenbu;
    getIdentity.fenxiao = fenxiao;
    getIdentity.zzname = this.data.zzname;
    getIdentity.zzid = this.data.zzid;
    getIdentity.erpdqm = this.data.erpdqm;
    this.setData({
      getIdentity: getIdentity
    })

    this.data.myPhone == '' ? '' : this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'))
    console.log(this.data)
    //查询是否注册并且返回user信息
    this.thisMethod()
  },
  m_select_touch: async function (t) {
    const { selIndex, selectText, stype, level } = t.detail;
    if(level == 1) {
      if(selIndex === 0 || selIndex === 1) {
        this.setData({
          xianquRst: ''
        })
      } else {
        if(!xianquArr) await this.getCityInfo();
        this.setData({
          xianquArr: xianquArr[selIndex]
        })
      }
      this.setData({
        areaRst: selectText == '不限' ? '' : selectText,
        cityIndex: selIndex
      })
    } else if (level == 6) { //非级联--个人选项
      stype == 'xianqu' ? this.setData({
        xianquRst: selectText == '不限' ? '' : selectText
      }) : stype == 'xueli' ? this.setData({
        xueliRst: selectText == '不限' ? '' : selectText
      }) : stype == 'xuewei' ? this.setData({
        xueweiRst: selectText == '无' ? '' : selectText
      }) : stype == 'zzmm' ? this.setData({
        zzmmRst: selectText == '不限' ? '不限' : selectText
      }) : stype == 'sex' ? this.setData({
        sexRst: selectText == '不限' ? '' : selectText
      }) : stype == 'area' ? this.setData({
        areaRst: selectText == '不限' ? '' : selectText
      }) : stype == 'sfyj' ? this.setData({
        sfyjRst: selectText == '不限' ? '' : selectText
      }) : this.setData({
        zldxRst: selectText == '不限' ? '' : selectText
      })
    } else if (level == 5) {
      this.setData({
          bklxvalue: selectText
      })
    }
  },
  // 多选选择持有证书
  getMultiSelect(e) {
    let { selectedTexts, stype, level, selectedValues } = e.detail;
    selectedTexts = selectedTexts.join(',')
    if(stype === 'xiangmu') {
      this.setData({
        xiangmuRst: selectedTexts
      })
    } else if(stype === 'zgzs') {
      this.setData({
        zgzsRst: selectedTexts
      })
    }
  },
  // 选择专业
  m_selectSearch_touch: function (t) {
    var e = t.detail.selText;
    // var e='专科-农林牧渔-农业类-现代农业技术';
    let _index2 = e.lastIndexOf('-') + 1;
    let _index0 = e.indexOf('-') + 1;
    let _index1 = e.indexOf('-', _index0);
    let zy2 = e.substr(_index2, e.length);
    let zy1 = e.slice(_index1 + 1, _index2 - 1);
    let zy0 = e.slice(_index0, _index1);
    this.setData({
      major: zy2 == '不限' ? '不限' : zy2,
      zylevel2: zy2 == '不限' ? '不限' : zy2,
      zylevel1: zy1,
      zylevel0: zy0,
      zydata: {
        'zy0': zy0,
        'zy1': zy1,
        'zy2': zy2,
      },
      zhuanyeFlag: zy2 == '不限' || zy2 == '' ? false : true,
    });
  },
  // 获取证书
  async getZsLists(){
    return new Promise(resolve => {
      jiatuiRequest({
        url: `/api/v1/moonshot/getZsListXcx`,
        method: 'GET',
        dataType: 'jsonp',
        header: {
          'X-Requested-From': 'WeChatMiniProgram'
        },
        data: {
          page: 1,
          pageSize: 200,
          status: 1,
          order: 'asc'
        },
      }).then(res => {
        let { code, data } = res;
        if(code == 0) {
          let { list } = data;
          this.setData({
            zgzsArr: list
          })
          resolve()
        } else {
          this.setData({
            zgzsArr: []
          })
          resolve()
        }
      })
    })
  },
  // 按个人条件查询
  async clickfn() {
    if(!this.data.major || this.data.major == '不限') {
      xhs.showToast({
        title: '请选择您的专业',
        icon: 'error'
      })
      return false;
    }
    this.setData({
      fypage: 1,
      zyLevel: 2
    })
    let data = {
      city: this.data.areaRst != undefined ? this.data.areaRst : '',
      xianqu: this.data.xianquRst != undefined ? this.data.xianquRst : '',
      xuewei: this.data.xueweiRst != undefined ? this.data.xueweiRst : '',
      xueli: this.data.xueliRst != undefined ? this.data.xueliRst : '',
      sex: this.data.sexRst != undefined ? this.data.sexRst : '',
      sfyj: this.data.sfyjRst != undefined ? this.data.sfyjRst : '',
      zzmm: this.data.zzmmRst != undefined ? this.data.zzmmRst : '',
      zgzs: this.data.zgzsRst != undefined ? this.data.zgzsRst : '',
      xiangmu: this.data.xiangmuRst != undefined ? this.data.xiangmuRst : '',
      major: this.data.major != undefined ? this.removeNumbersAndLetters(this.data.major) : '不限',
      limits: this.data.limits,
      page: 1,
    }
    this.setData({
      grdata: data,
      table: true,
      form: false,
      chaxunjg: 0,
      cxlists: [],
      allLists: [],
      onReachBottomlists: [],
      onReachBottomNum: this.data.onReachBottomNum + 1
    })
    await this.getUser()
    await this.chaxun(this.data.grdata)
  },
  // 按个人条件查询---专业--一级学科-专业大类--不限专业
  clickfn2: async function (t) {
    var e = t.currentTarget.dataset.zyjb;
    e == 'zy2' ? this.setData({
      zyLevel: 2
    }) : e == 'zy1' ? this.setData({
      zyLevel: 1
    }) : e == 'zy0' ? this.setData({
      zyLevel: 0
    }) : this.setData({
      zyLevel: 'bx'
    })
    if (e != 'zy00') {
      this.setData({
        onReachBottomNum: 0,
        major: this.data.zydata[e],
        ['grdata.major']: this.removeNumbersAndLetters(this.data.zydata[e]),
        ['grdata.page']: 1,
        page: 0,
        table: true,
        chaxunjg: 0,
        cxlists: [],
        allLists: [],
        onReachBottomlists: [],
        onReachBottomNum: this.data.onReachBottomNum + 1
      })
    } else {
      this.setData({
        onReachBottomNum: 0,
        major: '不限',
        ['grdata.major']: '不限',
        ['grdata.page']: 1,
        page: 0,
        table: true,
        chaxunjg: 0,
        cxlists: [],
        allLists: [],
        onReachBottomlists: [],
        onReachBottomNum: this.data.onReachBottomNum + 1
      })
    }
    await this.chaxun(this.data.grdata)
  },
  // 学员信息查询 
  async getUser() {
    if (!this.data.user) {
      await app.methods.registerCX(this.data.myPhone, this.data.options.actid, this.data.laiyuan, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, '', '', 'chaxun')
      this.setData({
        ['user.user.laiyuan']: this.data.laiyuan,
        ['xiangmuok']: false
      })
    } else {
      let laiyuan = this.data.user.user.laiyuan;
      if (laiyuan.indexOf(this.data.laiyuan) < 0) {
        await app.methods.updateuserCX(this.data.myPhone, this.data.options.actid, this.data.laiyuan, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, '', 'chaxun', '', this.data.user.user)
        this.setData({
          ['user.laiyuan']: this.data.laiyuan,
          ['xiangmuok']: false
        })
      }
    }
  },
  // 查询数据
  async chaxun(data) {
    console.log(this.data.grdata)
    return new Promise((resolve) => {
      jiatuiRequest({
        url: `/api/v1/moonshot/getZwkPositionsXcx`,
        type: 'GET',
        dataType: 'jsonp',
        header: {
          'X-Requested-From': 'WeChatMiniProgram'
        },
        data: this.data.grdata,
      }).then(res => {
        let { code, data } = res;
        if(code == 0) {
            let { result, total } = data;
            if(total == 0) return xhs.showToast({
              title: '暂无数据',
              icon: 'none',
              duration: 2000
            })
            this.setData({
              chaxunjg: total,
              zcpages: parseInt(total / this.data.limits) + 1,
              cxlists: this.data.cxlists.concat(result),
              allLists: result,
              onReachBottomlists: result,
              onReachBottomNum: this.data.onReachBottomNum + 1
            })
            resolve()
        } else {
            xhs.showToast({
              title: data.message,
              icon: 'none',
              duration: 2000
            })
            resolve()
        }
      })
    })
  },
  // 重新检索
  changetj: function () {
    this.setData({
      table: false,
      form: true,
      allLists: "",
      cxlists: "",
      yearvalue: "",
      addressvalue: "",
      xueliArrvalue: "",
      zzmmvalue: "",
      major: "",
      str1: "",
      str2: "",
      str3: "",
      ['zydata']: {},
      ['grdata']: {},
    })
  },
  // 触底刷新
  async onReachBottom() {
    if (this.data.table) {
      // 获取当前检索总页数---totleCont
      if (((this.data.chaxunjg) % this.data.limits) == 0) {
        var totleCont = parseInt(this.data.chaxunjg / this.data.limits)
      } else {
        var totleCont = parseInt(this.data.chaxunjg / this.data.limits) + 1
      }
      // 计算当前已经展示几屏--当前页码--nowCont
      if (this.data.cxlists.length % this.data.limits == 0) {
        var nowCont = parseInt(this.data.cxlists.length / this.data.limits)
      } else {
        var nowCont = parseInt(this.data.cxlists.length / this.data.limits) + 1
      }
      // 判断是否已经全部加载完
      if ((((this.data.chaxunjg) % this.data.limits) == 0) && (totleCont == nowCont)) {
        xhs.showToast({
          title: '我是有底线哒',
          duration: 2000
        })
      } else if ((((this.data.chaxunjg) % this.data.limits) > 0) && (totleCont < (nowCont + 1))) {
        xhs.showToast({
          title: '我是有底线哒',
          duration: 2000
        })
      } else {
        // 注入新数据---重新走接口
        this.setData({
          fypage: this.data.fypage + 1,
          ['grdata.page']: this.data.fypage + 1
        })
        await this.chaxun(this.data.grdata)
      }
    }
  },
  // 收藏/取消收藏
  collect: async function (e) {
    if (this.data.myPhone != '') {
      let { ggid, zwid, index, action } = e.currentTarget.dataset;
      xhs.showLoading({
        title: action == 'collect' ? '收藏中！' : '取消收藏中！',
        icon: 'none',
        duration: 2000,
      })
      let { cxlists } = this.data;
      let data = {
        ggid,
        zwid,
        action,
        phone: this.data.myPhone,
      }
      let collectData = await this.updateCollect(data)
      console.log(collectData)
      xhs.hideLoading({})
      xhs.showToast({
        title: collectData == 1 ? action == 'collect' ? '添加收藏成功!' : '取消收藏成功!' : '操作失败',
        icon: 'success',
        duration: 1500
      })
      if(collectData == 1) {
        action == 'collect' ? cxlists[index].collect = true : cxlists[index].collect = false;
        this.setData({
          cxlists
        })
      }
    } else {
      xhs.showToast({
        title: '请先注册',
        duration: 2000
      })
      if (app.globalData.PrivacyAuthorization) {
        this.setData({
          isShowAuth: !0,
          isPrivacyAuthorization: !0,
        })
      } else {
        this.setData({
          isShowAuth: !0,
          isPrivacyAuthorization: !1,
        })
      }
      return false
    }


  },

  // 收藏和取消的方法
  updateCollect(searchData) {
    return new Promise((resolve) => {
      jiatuiRequest({
        url: `/api/v1/moonshot/collectPosition`,
        method: 'post',
        dataType: 'jsonp',
        header: {
          'X-Requested-From': 'WeChatMiniProgram'
        },
        data: searchData,
      }).then(res => {
        let { code, data } = res;
        if (code == 0) {
          resolve(1)
        } else {
          resolve(0)
        }
      }).catch(err => {
        console.log(err)
        resolve(0)
      })
    })
  },
  //去往个人收藏
  goToMyCenter: function () {
    const { options } = this.data;
    xhs.navigateTo({
      url: `./center?id=${options.id}&gh=${options.gh}&fx=${options.fx}`,
    })
  },
  // 公告查看
  gonggao: function (e) {
    const { notice_url } = e.currentTarget.dataset;
    xhs.navigateTo({
      url: `/packageB/gg/gg?link=${notice_url}`,
    })
  },
  // 回到顶部
  toTop() {
    xhs.pageScrollTo({
      scrollTop: 0
    })
  },
  // 该怎么就怎么
  thisMethod: async function () {
    if(this.data.myPhone) {
      if (!this.data.user) {
        await app.methods.registerCX(this.data.myPhone, this.data.options.actid, this.data.laiyuan, this.data.isSydw, this.data.getIdentity, this.data.bklxvalue, '', '', 'chaxun')
        this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'))
      } else {
        await app.methods.updateuserCX(this.data.myPhone, this.data.options.actid, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.user.user.gwlb, '', 'chaxun', this.data.book, this.data.user.user)
        this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'))
      }
    }
  },
  // 展开和收起
  toggle: function (e) {
    const { index } = e.currentTarget.dataset;
    let { cxlists } = this.data;
    cxlists[index].toggle = !cxlists[index].toggle
    this.setData({
      cxlists
    })
  },
  // 处理专业代码
  removeNumbersAndLetters(str) {
    return str.replace(/[a-zA-Z0-9]/g, '');
  },
  // 监听手机号
  async user_login(e){
    if(typeof e.detail == 'boolean') {
        this.setData({
            isShowAuth: e.detail
        })
    } else {
        this.setData({
            myPhone: e.detail
        })
        this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'));
        await this.thisMethod();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成---从个人中心页面返回，
   */
  async onShow() {
    if (this.data.grdata.limits == this.data.limits) {
      this.setData({
        cxlists: [],
        collectLists: [],
        fypage: 1,
        ['grdata.page']: 1
      })
      await this.chaxun(this.data.grdata)
    } else {

    }


  },

  /**
   * 获取各县区数据
   */
  getCityInfo(){
    return new Promise(resolve => {
      jiatuiRequest({
        url: '/api/v1/files/getFile',
        data: {
          fileName: 'city'
        },
        header: {
            'X-Requested-From': 'WeChatMiniProgram'
        },
      }).then(res => {
        console.log(res)
        if(res && res.data && res.code == 0) {
          xianquArr = res.data.jsonData
        } else {
          wx.showToast({
            title: '县区加载失败',
            icon: 'error'
          })
        }
        resolve()
      }).catch(err => {
        console.log(err)
        wx.showToast({
          title: '县区加载失败',
          icon: 'error'
        })
        resolve()
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let { fx = 'hbwtb', gh = 'zcj37368', id } = this.data.options;
    console.log(`/packageB/zwk/index?id=${id}&fx=${fx}&gh=${gh}`)
    return {
      title: this.data.laiyuan,
      path: `/packageB/zwk/index?id=${id}&fx=${fx}&gh=${gh}`,
      imageUrl: 'https://statics.offcn.com/hb/pc/images/wxxcx/shareImg/skzwjs.jpg',
    }
  }
})